//
// ----------------------------------------------------------------------------
// 文件： Logging.h
// 日期： 2018年08月29日
// 功能： 日志与通用工具模块
// 版本： v2.0
// 作者： 谈新
// 邮件： tanxin@smg.cn
// ----------------------------------------------------------------------------
//
#ifndef _LOGGING_H_INCLUDE
#define _LOGGING_H_INCLUDE

#ifdef __cplusplus
extern "C" {
#endif

#include <stdlib.h>

#if defined(LINUX)
    #include <netinet/in.h>
    #include <net/if.h>
    #include <unistd.h>
#else
    #include <ws2tcpip.h>
#endif

#if !defined(LINUX)
extern HWND             hMainWnd;                // 程序窗体的句柄
#endif

#if !defined(LINUX)
extern	 CListBox       *pList;
#endif

// 日志级别定义
#define  LEVEL_FATAL         0                   // 致命错误
#define  LEVEL_ERROR         1                   // 一般性错误
#define  LEVEL_WARN          2                   // 警告信息 
#define  LEVEL_HINT          3                   // 提示信息  
#define  LEVEL_DEBUG         4                   // 调试信息    
#define  LFATAL              __LINE__, __FILE__, LEVEL_FATAL
#define  LERROR              __LINE__, __FILE__, LEVEL_ERROR
#define  LWARN               __LINE__, __FILE__, LEVEL_WARN
#define  LHINT               __LINE__, __FILE__, LEVEL_HINT
#define  LDEBUG              __LINE__, __FILE__, LEVEL_DEBUG

extern short            gNetNum;
extern char             gNetName[20][250];
extern char             gNetIp[20][20];
extern char             gNetMac[20][20];

int            InitLogging(int isExit,void (*func)(char *file));  // isExit ： 0 没有找到配置文件，也不会退出程序  !0 没有找到配置文件，程序就退出。
int            DestroyLogging();
int            GetLogInit();
void           WriteFileLog( int eiLine, char *epczFile, int eiLevel, char *epczFmt, ...);
void           ProgramStartsRecording();
char           *GetAppVersion();
void           SetVersion(char *ver);
int            GetBigEndian();
int            GetLogLevel();
void           SetLogLevel( int lev );
int            GetLogMode();
void           SetLogMode( int out );
short          GetLogIpNum();
void           SetLogIpNum( short num );
unsigned short GetLogPort();
void           SetLogPort( unsigned short port );
char           *GetConfigFile();
char           *GetLogDir();
void           SetLogDir(char *dir);
int            GetDelete();
void           SetDelete( int is );
long           GetDeleteTime();
void           SetDeleteTime( int day );

char       *GetAppPath();
char       *GetAppName();
char       *GetAppIp();
char       *GetAppMac();
double     GetTimeVal();
char       *GetDateTimeString(time_t now);
char       *GetTimeString(double now);
char       *GetTimeDelay(int delay);
time_t     string2time(const char * str);
int        IncludeChinese(char *str);
int        PreCreatePath(const char *path);
int        PreCreatefile(const char *filename);
int        DeleteFileByName(const char *file);
int        isPathExists(const char *path);
int        isProcessExists(const char *name);
int        GetSubstr(char *sou,char *begin,char *end,char *des,long maxlen);
int	   Split(char *src, const char *separator, char **dest, int *num);
char       *replace (const char *str, const char *src, const char *dst);
int        ReadCfgItem( char const *file,char const *item,char *buf);
int        CheckBigEndian();
int        CheckIPCorrect(char *ip);
int        GetFileSizeByName(const char* file);

void       WaitNetworkUp();
int        GetNetworkInfo();
void       DisplayNetworkInfo();

           #if defined(LINUX)
pthread_t  CreateThreadWithArg(void *(*func)(void *), void *arg);
           #else
HANDLE     CreateThreadWithArg(void *(*func)(void *), void *arg);
           #endif

// 编码转换
int        GetUtf8Length(const char *str);
char       *GB18030ToUTF_8(const char *pGBKstr,int gbkLen,char *pUTF8str,int *utf8Len);
char       *UTF_8ToGB18030(const char *pUTF8str,int utf8Len,char *pGBKstr,int *gbkLen);
void       GB18030ToUnicode(wchar_t* pOut,const char *gbBuffer);
void       UnicodeToUTF_8(char *pOut,wchar_t *pText);
void       UnicodeToGB18030(char* pOut,unsigned short* uData);
void       UTF_8ToUnicode(wchar_t* pOut,const char *pText);
int        URLEncode(const char* str, const int strSize, char* result, const int resultSize);

long long  myatoll(char *instr);               // 字符串转long long类型
char       *mytrim(char *str);                 // 字符串头尾修剪函数
char       *mytrim1(char *str);                // 字符串头尾修剪函数
char       *JsonIllegalCharFilter(char *s);    // 与Json不兼容的特殊字符过滤函数
char       *stringFilter(char *sou,char *sfi); // 从字符串中过滤字符串
int        CheckJsonIllegalChar(char *s);      // 检查是否有不兼容json的字符
char       *GetFileName(char *path);           // 从路径名获取返回文件名

int        StringToHex(const char *src, int len, char *dst, int maxlen);
int        HexToString(const char *src, int len, char *dst, int maxlen);

void       GetAddressNumeric(const char *ip, unsigned char *i1, unsigned char *i2, unsigned char *i3, unsigned char *i4);

int        Accept(int fd, struct sockaddr *sa, socklen_t *salenptr,int *loop);
void       Bind(int fd, const struct sockaddr *sa, socklen_t salen);
void       Connect(int fd, const struct sockaddr *sa, socklen_t salen);
void       Getsockopt(int fd, int level, int optname, void *optval, socklen_t *optlenptr);
void       Setsockopt(int fd, int level, int optname, const void *optval, socklen_t optlen);
void       Listen(int fd, int backlog);
void       Shutdown(int fd, int how);
int        Socket(int family, int type, int protocol);
void       *Calloc(size_t n, size_t size);
void       Close(int fd);
int        Fcntl(int fd, int cmd, int arg);
int        Ioctl(int fd, int request, void *arg);
void       *Malloc(size_t size);
int        Tcp_listen(const char *host, const char *serv, socklen_t *addrlenp);
int        Tcp_connect(const char *host, const char *serv);
int        Udp_connect(const char *host, const char *serv,struct sockaddr *toaddr,socklen_t *tolen);
int        mcast_join(int sockfd, const struct sockaddr *grp, socklen_t grplen,const char *ifname, u_int ifindex);
int        mcast_join_source_group(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen,const char *ifname, u_int ifindex);
int        mcast_block_source(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen);
int        mcast_unblock_source(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen);
int        mcast_set_ttl(int sockfd, int val);
int        sockfd_to_family(int sockfd);
int        family_to_level(int family);
int        SetNonblockSocket(int fd);
int        SetSOLINGEROn(int fd);
size_t     Writen(int fd, const void *vptr, size_t n);

int        udp_client(const char *host, const char *serv, struct sockaddr *saptr, socklen_t *lenp);
int        udp_server(const char *host, const char *serv, socklen_t *addrlenp);
int        mcast_join(int sockfd, const struct sockaddr *grp, socklen_t grplen,const char *ifname, u_int ifindex);
int        mcast_join_source_group(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen,const char *ifname, u_int ifindex);
int        mcast_block_source(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen);
int        mcast_unblock_source(int sockfd, const struct sockaddr *src, socklen_t srclen,const struct sockaddr *grp, socklen_t grplen);
int        mcast_set_loop(int fd, int onoff);
int        family_to_level(int family);
int        sockfd_to_family(int sockfd);
char       *sock_ntop(const struct sockaddr *sa, socklen_t salen);

#ifdef __cplusplus
}
#endif

#endif 

